package com.time.workshop.utils;

import java.util.List;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.time.workshop.R;
import com.time.workshop.adapter.TitleAdapter;

public class TitlePopupwindow {
	
	public PopupWindow mPopupWindow_Area;
	private Activity mActivity;
	private List<String> mList;
	private IDropdownItemClickListener mItemClickListener;
	public TitlePopupwindow(Activity activity,List<String> list){
		this.mActivity = activity;
		this.mList = list;
	}
	
	public void showAt(int[] location, int width,int height){
		if(mPopupWindow_Area == null){
			LayoutInflater inflater = mActivity.getLayoutInflater();
			View contentView = inflater.inflate(R.layout.title_popup_dropdown, null);
			ListView listView = (ListView) contentView.findViewById(R.id.title_lv);
			TextView view = (TextView) contentView.findViewById(R.id.view);
			TitleAdapter adapter = new TitleAdapter(mActivity, mList, R.layout.title_adapter);
			listView.setAdapter(adapter);
			mPopupWindow_Area = new PopupWindow(contentView, width,height-location[1]+25);
//			mPopupWindow_Area.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.btn_back_pre));
			mPopupWindow_Area.setTouchable(true);
			mPopupWindow_Area.setOutsideTouchable(true);
			mPopupWindow_Area.setFocusable(true);// 设置外部可点击
			mPopupWindow_Area.update();
			listView.setOnItemClickListener(mAreaDropItemListener);
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					mPopupWindow_Area.dismiss();
				}
			});
			mPopupWindow_Area.setAnimationStyle(R.style.TypeSelAnimationFade);
			mPopupWindow_Area.showAtLocation(mActivity.getWindow()
					.getDecorView(), Gravity.NO_GRAVITY, location[0], location[1]+25);
		}
	}
	
	public void setItemClickListener(IDropdownItemClickListener listener){
		mItemClickListener = listener;
	}
	
	@SuppressWarnings("unused")
	private View.OnClickListener mAreaDropdownItemClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(mPopupWindow_Area != null && mPopupWindow_Area.isShowing()){
				mPopupWindow_Area.dismiss();
			}
			if(mItemClickListener != null){
				TextView tv = (TextView) v;
				mItemClickListener.onItemClick(tv.getText().toString(),-1);
			}
		}
	};
	
	private OnItemClickListener mAreaDropItemListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(mPopupWindow_Area != null && mPopupWindow_Area.isShowing()){
				mPopupWindow_Area.dismiss();
			}
			if(mItemClickListener != null){
				mItemClickListener.onItemClick(""+mList.get(arg2),arg2);
			}
		}
	};

}
